package com.xkcoding.rpc.server;


import com.xkcoding.rpc.client.Calculator;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * RPC 远程过程调用的Server端，
 * Server端主要是通过ServerSocket的accept方法，来接收Client端的请求，接着就是反序列化请求->执行->序列化执行结果，最后将二进制格式的执行结果返回给Client。
 */
public class ProviderApp {
    private Calculator calculator = new CalculatorImpl();

    public static void main(String[] args) throws IOException {
        new ProviderApp().run();
    }

    private void run() throws IOException {
//        ServerSocket listener = new ServerSocket(9090);
//        try {
//            while (true) {
//                Socket socket = listener.accept();
//                try {
//                    // 将请求反序列化
//                    ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
//                    Object object = objectInputStream.readObject();
//
//                    System.out.println("request is "+object);
//
//                    // 调用服务
//                    int result = 0;
//                    if (object instanceof CalculateRpcRequest) {
//                        CalculateRpcRequest calculateRpcRequest = (CalculateRpcRequest) object;
//                        if ("add".equals(calculateRpcRequest.getMethod())) {
//                            result = calculator.add(calculateRpcRequest.getA(), calculateRpcRequest.getB());
//                        } else {
//                            throw new UnsupportedOperationException();
//                        }
//                    }
//
//                    // 返回结果
//                    ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
//                    objectOutputStream.writeObject(new Integer(result));
//                } catch (Exception e) {
//                    System.err.println(e.getMessage());
//                } finally {
//                    socket.close();
//                }
//            }
//        } finally {
//            listener.close();
//        }
    }

}
